package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.entity.TbProjectInfoDo;
import com.yxx.business.entity.TbProjectInfoVo;
import com.yxx.business.entity.TbProjectInfoQueryDto;
import com.yxx.business.entity.TbProjectInfoEditDto;
import com.yxx.business.service.ITbProjectInfoService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 项目信息Controller
 *
 * @author yxx
 * @date 2025-08-03
 */
@Tag(name = "项目信息管理-TbProjectInfo")
@RestController
@RequestMapping("/business/projectInfo")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbProjectInfoController extends BaseControllerPlus {

    private final ITbProjectInfoService tbProjectInfoService;

    @Operation(summary = "查询--项目信息列表")
    @PreAuthorize("@ss.hasPermi('business:projectInfo:list')")
    @GetMapping("/list")
    public PageResult<TbProjectInfoVo> list(@ParameterObject TbProjectInfoQueryDto tbProjectInfoQueryDto) {
        return tbProjectInfoService.selectTbProjectInfoVoPage(tbProjectInfoQueryDto);
    }

    @Operation(summary = "查询--项目信息单个")
    @PreAuthorize("@ss.hasPermi('business:projectInfo:query')")
    @GetMapping(value = "/{projectId}")
    public R<TbProjectInfoVo> getInfo(@PathVariable Long projectId) {
        return R.ok(tbProjectInfoService.selectTbProjectInfoVoByProjectId(projectId));
    }

    @Operation(summary = "新增--项目信息")
    @PreAuthorize("@ss.hasPermi('business:projectInfo:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody @Validated TbProjectInfoEditDto tbProjectInfoQueryDto) {
        return toResult(tbProjectInfoService.insertTbProjectInfo(tbProjectInfoQueryDto));
    }

    @Operation(summary = "修改--项目信息")
    @PreAuthorize("@ss.hasPermi('business:projectInfo:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping("/{projectId}")
    public R<String> update(@PathVariable Long projectId, @RequestBody @Validated TbProjectInfoEditDto tbProjectInfoQueryDto) {
        return toResult(tbProjectInfoService.updateTbProjectInfo(projectId, tbProjectInfoQueryDto));
    }

    @Operation(summary = "删除--项目信息")
    @PreAuthorize("@ss.hasPermi('business:projectInfo:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public R<String> delete(@PathVariable List<Long> projectIds) {
        return toResult(tbProjectInfoService.deleteTbProjectInfoByProjectIds(projectIds));
    }

    @Operation(summary = "导出--项目信息列表")
    @PreAuthorize("@ss.hasPermi('business:projectInfo:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbProjectInfoQueryDto tbProjectInfoQueryDto) {
        List<TbProjectInfoDo> list = tbProjectInfoService.selectTbProjectInfoDoList(tbProjectInfoQueryDto);
        ExcelUtil<TbProjectInfoDo> util = new ExcelUtil<>(TbProjectInfoDo.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    @Operation(summary = "导出--项目信息模板")
    @PreAuthorize("@ss.hasPermi('business:projectInfo:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbProjectInfoDo> util = new ExcelUtil<>(TbProjectInfoDo.class);
        util.importTemplateExcel(response, "导入项目信息模板数据");
    }

    @Operation(summary = "导入--项目信息列表")
    @PreAuthorize("@ss.hasPermi('business:projectInfo:import')")
    @Log(title = "项目信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbProjectInfoDo> util = new ExcelUtil<>(TbProjectInfoDo.class);
        List<TbProjectInfoDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbProjectInfoService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }
}
